package com.xzx.spark.core.transform

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 *
 * ${DESCRIPTION}
 *
 * @author xinzhixuan
 * @version 1.0
 * @date 2021-06-26 8:39 下午
 */
object Spark004_FlatMap {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setMaster("local[*]").setAppName("Spark004_FlatMap")
    val context = new SparkContext(conf)
    val rdd: RDD[List[Int]] = context.makeRDD(List(List(1, 2), List(4, 5)))
    val mapRDD: RDD[Int] = rdd.flatMap(list => list)
    mapRDD.collect().foreach(println)
    println("================================")
    val rdd1 = context.makeRDD(List(List(1.0, 2), 3.0, List(4, 5)))
    val flatMapRDD = rdd1.flatMap {
      case list: List[_] => list
      case x => List(x)
    }
    flatMapRDD.collect().foreach(println)

    context.stop()
  }
}
